﻿

//zTree setting设置
var zTreeObj;
var setting = {
    async: {
        enable: true,
        contentType: "application/json",
        dataType: "json",
        url: "/Role/GetMenuRole?RoleId=" + window.location.href.split('=')[1],
        type: "post"
    },
    check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "ps", "N": "ps" }
    },
    data: {
        key: {
            name: "MenuName",
            title: "RoleId"
        },
        simpleData: {
            enable: true,
            idKey: "Id",
            pIdKey: "ParentId"

        }
    },
    callback: {
        onAsyncSuccess: zTreeOnAsyncSuccess
    }
};

$(function () {
    //alert(setting);
    zTreeObj = $.fn.zTree.init($("#treeMenu"), setting);
});

function zTreeOnAsyncSuccess(event, treeId, treeNode) {
    zTreeObj.expandAll(true);

    var nodes = zTreeObj.transformToArray(zTreeObj.getNodes());

    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].RoleId.length > 0) {
            zTreeObj.checkNode(nodes[i], true, false);
        }
    }
};


//取消按钮方法
function Cancel() {
    location.href = "/Role/Index";
}

//保存按钮方法
function Save() {
    if (!$("#roleForm").valid()) {
        return;
    }

    var nodes = zTreeObj.getCheckedNodes(true);
    var Entitle = "";
    for (var i = 0; i < nodes.length; i++) {
        Entitle += nodes[i].Id + ";";
    }

    $.post("/Role/UpdateRole", {
        Id: $("#Id").val(),
        RoleName: $("#RoleName").val(),
        Description: $("#Description").val(),
        MenuId: Entitle
    }, function (data) {
        if (data == "True") {
            alert(SaveSuccess);
            window.location.href = "/Role/Index";
        }
        else {
            alert(SaveFail);
        }
    });
}
